require(['./config'], () => {
    require(['jquery'], () => {
        var signUpButton = document.getElementById('signUp')
        var signInButton = document.getElementById('signIn')
        var container = document.getElementById('dowebok')

        signUpButton.addEventListener('click', function () {
            container.classList.add('right-panel-active')
        })

        signInButton.addEventListener('click', function () {
            container.classList.remove('right-panel-active')
        })
    })
})

//onblur失去焦点事件，用户离开输入框时执行 JavaScript 代码：
//函数1：验证用户名格式
function validate_username(username) {

    if (username != "") {
        document.getElementById("test_user").innerHTML = "<font color='green' size='3px'>√用户名正确</font>";
        return true
    } else {
        document.getElementById("test_user").innerHTML = "<font color='red' size='3px'>用户名错误</font>";
        return false
    }
}

//函数2：验证密码是否符合要求
function validate_password(password) {
    //^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6-10}$
    var passwordReg = /^[a-zA-Z]\w{5,17}$/;
    if (password != "" && password.search(passwordReg) != -1) {
        document.getElementById("test_pw").innerHTML = "<font color='green' size='3px'>√密码格式正确</font>";
        return true;
    } else {
        document.getElementById("test_pw").innerHTML = "<font color='red' size='3px'>密码由数字、字母组成，不少于6位</font>";
        // alert("密码由数字和字母组成!");
        return false;
    }
}

//函数3：验证两次输入的密码是否一样
function validate_password2(password2) {

    let password = document.getElementById("Password").value;
    // console.log(password);
    if (password == "") {
        document.getElementById("is_test_pw").innerHTML = "<font color='red' size='3px'>密码不为空</font>";
        return false;
    } else if (password == password2) {
        document.getElementById("is_test_pw").innerHTML = "<font color='green' size='3px'>√两次输入的密码相同</font>";
        return true;
    } else {
        document.getElementById("is_test_pw").innerHTML = "<font color='red' size='3px'>两次输入的密码不相同</font>";
        return false;
        // console.log("密码由数字和字母组成!");
    }
}
//7天免登录
require(['./config'], () => {
    require(['myCookie'], () => {
        let login = document.querySelector("#login")
        let user = document.querySelector(".user")
        let pass = document.querySelector(".pass")
        let register = document.querySelector("#register")
        let ckb = document.querySelector("#wy")

        function fn() {
            if (getCookie("userName") && getCookie("passWord")) {

                user.value = getCookie("userName")
                pass.value = getCookie("passWord")
            }
        }
        fn()

        //点击登录
        login.addEventListener("click", () => {
            // console.log("1")
            let type = ckb.checked
            // console.log(type)
            if (user.value && pass.value) {
                //通过type的值来设置cookie的过期时间
                if (type) {
                    setCookie("userName", user.value, 7)
                    setCookie("passWord", pass.value, 7)
                } else {
                    setCookie("userName", user.value)
                }
                alert("登录成功")
                location.href = "../index.html"
            } else {
                alert("用户名密码不能为空")
            }
        }, false)

        register.addEventListener("click", () => {
            let user = document.querySelector("#Username")
            let pass = document.querySelector("#Password")
            let pass2 = document.querySelector("#Password2")
            console.log(validate_username(user.value))
            console.log(validate_password(pass.value))
            console.log(validate_password2(pass2.value))
            if (validate_username(user.value) && validate_password(pass.value) && validate_password2(pass2.value)) {
                setCookie("userName", user.value, 7)
                setCookie("passWord", pass.value, 7)
                alert("注册成功")
                location.href = "../index.html"
            }
        }, false)


    })
})